package com.xt.website_manager.dao;

import com.xt.website_manager.po.Product;
import com.xt.website_manager.po.ProductFile;
import com.xt.website_manager.util.MyPageInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @Author: jiangyu
 * @Date: 2019/8/18 14:07
 * @Description:
 */
@Mapper
public interface ProductDao {

    /**
     *  查询全部产品
     */
    @Select("<script>select p.* from tb_product p <if test='cid != null and cid != -1'> inner join tb_categorysecond cs on p.csid = cs.id inner join tb_category c on cs.cid = c.id</if><where><if test='cid != null and cid != -1'> and c.id = #{cid}</if><if test='searchCsid != null and searchCsid != -1'> and p.csid = #{searchCsid}</if><if test='searchProName != null'> and p.pro_name like '%${searchProName}%'</if></where> order by p.create_time desc</script>")
    @Results(id="productMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "pro_name", property = "proName"),
            @Result(column = "pro_picture", property = "proPicture"),
            @Result(column = "pro_info", property = "proInfo"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "pro_href", property = "proHref"),
            @Result(column = "pro_kind", property = "proKind"),
            @Result(column = "csid", property = "csid")
    })
    List<Product> findAllProducts(MyPageInfo<Product> pageInfo);

    /**
     * 查询产品详情
     */
    @Select("select * from tb_product where id = #{pid}")
    @ResultMap("productMap")
    Product productDetails(Integer pid);

    /**
     *  删除产品
     */
    @Delete("delete from tb_product where id = #{id}")
    void deleteProduct(Integer id);




    /**
     *  添加产品
     */
    @Insert("insert into tb_product values(default,#{proName},#{proPicture},#{proInfo},#{createTime},#{proHref},default,#{csid})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addProduct(Product product);

    /**
     *  删除产品的主图
     */
    @Update("update tb_product set pro_picture=null where id=#{pid}")
    void delProPic(Integer pid);

    /**
     * 修改产品信息
     */
    @Update("<script>UPDATE tb_product set pro_name=#{proName},pro_info=#{proInfo},pro_href=#{proHref},csid=#{csid}<if test='proPicture != null ' > ,pro_picture=#{proPicture}</if><where>id=#{id}</where></script>")
    void updateProduct(Product product);


    /**
     * ***************************以下为附件表操作****************************
     */

    /**
     *  查询产品附件详情
     */
    @Select("select * from tb_product_file where id=#{id}")
    ProductFile productFileDetails(Integer id);

    /**
     *  添加产品附件
     */
    @Insert("insert into tb_product_file values(default,#{image},#{pid})")
    void addProductFile(ProductFile productFile);

    /**
     * 根据pid删除附件
     */
    @Delete("delete from tb_product_file where pid = #{pid}")
    void deleteProFileByPid(Integer pid);

    /**
     * 根据主键id删除附件
     */
    @Delete("delete from tb_product_file where id = #{id}")
    void deleteProFile(Integer id);


    /**
     * 根据pid查询全部附件
     */
    @Select("select * from tb_product_file where pid=#{pid}")
    List<ProductFile> findProductFiles(Integer pid);


    /**
     * 根据二级类目id查询商品总数
     */
    @Select("select count(0) from tb_product where csid = #{csid}")
    Integer getNewsCount(Integer csid);
}
